package com.godman.liu.loggingdemo.controller;

import com.godman.liu.loggingdemo.async.AsyncTask;
import com.godman.liu.loggingdemo.event.MyApplicationEvent;
import com.godman.liu.loggingdemo.event.MyEventListener;
import com.google.gson.Gson;
import com.sun.corba.se.impl.orbutil.threadpool.ThreadPoolImpl;
import com.sun.corba.se.spi.orbutil.threadpool.ThreadPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/person")
@Slf4j
public class PersonCtrl {

    @Value("${state.vivi}")
    private String vivi;

    @Value("${state.isnow}")
    private boolean isnow;

    public static int mCount = 0;

    @Resource
    AsyncTask asyncTask;

    @GetMapping(value = "/name")
    public String sayHello(@RequestParam String userName,HttpServletRequest request) {
        //log.info("/person/name-"+PersonCtrl.class.getName());

        System.out.println(request.getHeader("User-Agent"));

        log.info("/person/vivi-" + vivi);

        log.info("/person/isnow-" + isnow);

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //增加监听器
        context.addApplicationListener(new MyEventListener());
        //上下文启动
        context.refresh();
        //发布事件
        MyApplicationEvent myApplicationEvent = new MyApplicationEvent("This is an event！");
        myApplicationEvent.setEventName("454547");
        context.publishEvent(myApplicationEvent);

        //检测日志配置文件是否是debug级别，不是debug运行下打印信息
        if(log.isDebugEnabled()){
            log.debug("我就是debug日志");
        }else{
            log.info("我是运行日志");
        }

        return "Hi," + userName;
    }

    @PostMapping(value = "/addname")
    public String addname(@RequestParam Map<String, String> params) {
        log.info(params.get("name"));
        return "success";
    }

    @PostMapping(value = "/postname",produces = {"application/json"})
    public String postname(String name, Integer age, HttpServletRequest request) {


        System.out.println(request.getHeader("User-Agent"));

        log.info("name:{},age:{}",name,age);
        Map<String,Object> result = new HashMap<>();
        result.put("name",name);
        result.put("age",age);
        return new Gson().toJson(result);
    }

    @PostMapping(value = "/newname",produces = {"application/json"})
    public String newname(@RequestBody Map<String, String> params) {
        log.info("name:{},age:{},sex:{}",params.get("name"),params.get("age"),params.get("sex"));
        Map<String,Object> result = new HashMap<>();
        result.put("name",params.get("name"));
        result.put("age",params.get("age"));
        result.put("sex",params.get("sex"));
        return new Gson().toJson(result);
    }

    @PutMapping(value = {"putmethod"}, produces = {"application/json;charset=UTF-8"})
    public String testPut(String id, int count) {
        if (id.equals("1")) {
            mCount += count;
        }
        Map<String, String> result = new HashMap<>();
        result.put("count", mCount + "");
        return new Gson().toJson(result);
    }

    @GetMapping(value = "",produces = {"application/json;charset=UTF-8"})
    public String getMethod(){
        return "当别的路径都对应不上时，进入这个方法";
    }

    @PostMapping(value = "",produces = {"application/json;charset=UTF-8"})
    public String postMethod(){
        return "当别的路径都对应不上时，进入这个方法";
    }

    @GetMapping("/params")
    public String someParams(@RequestParam String name,Integer age){
        return "hello:"+name+",age:"+age;
    }
    @PostMapping("/pp")
    public String pp(@RequestParam String name,Integer age){
        return "hello:"+name+",age:"+age;
    }

    @GetMapping(value = "/async")
    public String async(){

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                asyncTask.sayHello();
//            }
//        }).start();

        asyncTask.sayHello();


        return "mainfinish";
    }
}
